﻿var currentRoomUser = "";
var currentRoomUserTo = "";
var parentClose = false;
var _MsgDivHeader = "<div id=\"MsgDiv\" style =\"  overflow :hidden ; width:100%\">";
var _MsgDivFooter = "</div>";
var _MsgEndHTML = "<div id=\"2ffa7bd7259343babdfae91f9d2250\" style =\" overflow :hidden ; height :0px;\"></div>";
var _MsgNotOnLineTip = '<div style ="vertical-align:middle; padding-left:5px;color:blue;padding-top:5px; font-size:small ;"><img src="../img/notuser.ico"/><span style="padding-bottom:10px;">&nbsp;该好友不在线，您可以留言！</span></div>';
var currentChatInfo = "";
var currentUserToOnline = false;
var resizeCount = 0;

function init(isload) {

    var chatdiv = $(".chatDiv")[0];
    var chatbodytable = $(".chatbodyTable")[0];
    var chatbodydiv = $(".chatbodyDiv")[0];

    chatbodydiv.style.width = document.body.offsetWidth + "px";
    chatbodytable.style.width = parseInt(chatbodydiv.style.width) - 10+"px";
    chatbodytable.style.margin = "0px 0px 0px 0px";
    chatbodytable.style.padding = "0px 0px 0px 0px"; 
       
    var chatbodyTrList = chatbodytable.getElementsByTagName("tr");
    chatbodyTrList[0].style.height = "5px";
    chatbodyTrList[2].style.height = "5px";
    
    var chatbodyleftTd = $("#chatbodyLeftTd")[0];
    var chatbodymiddleTdImg = $("#chatbodyMiddleTd > img")[0];

    var width = "";


    if (chatbodymiddleTdImg.title == "点击隐藏") {
        width = parseInt(chatbodydiv.style.width) - 10-150 + "px";
    }
    else {
        width = parseInt(chatbodydiv.style.width) - 10-20 + "px";
    }
    //方法来自 editor
    setEditorDivClass(width, "none");
    
    var chatbodydisplaydiv = $("#chatbodydisplayDiv")[0];
    chatbodydisplaydiv.style.width = width;
    
    var chatbodyeditordiv = $("#chatbodyeditorDiv")[0]; 
    var chatbodysendMsgdiv = $("#chatbodysendMsgDiv")[0]; 
    var chatbodydivheight = 0 ;
    var totalHeight = 0;
    
    if (parseInt(document.documentElement.clientHeight) > 400) {
        totalHeight = parseInt(document.documentElement.clientHeight);
    }
    else {
        totalHeight = 400;
    }
    var otherHeight = $(".chatnavDiv")[0].scrollHeight + $(".chattoolDiv")[0].scrollHeight+15;
    $(".chatbodyDiv")[0].style.height = totalHeight - otherHeight + "px";
    chatbodydivheight = totalHeight - otherHeight;
    chatbodyleftTd.style.height = chatbodydivheight - 15+"px";  //15,12
    chatbodydisplaydiv.style.height = chatbodydivheight - 12 - getEditorHeight() - parseInt(chatbodysendMsgdiv.style.height)+"px";
    if (SysAgent.ie &&!SysAgent.ie)
        chatbodydisplaydiv.style.height = parseInt(chatbodydisplaydiv.style.height) - 3 + "px";

    //隐藏 快捷键设置按钮
//    if (SysAgent.firefox) {
//        $("#hotKeysButton")[0].style.display = "none";
//        $("#sendMessageButton")[0].disabled = false;
//    }

    if (isload == true) {
        try {
            // 读取和设置当前用户和聊天对象
            var result = chatblog.usercate.chRoom.GetCurrentUserID();
            if (result != null && result.value != null)
                currentRoomUser = result.value;

            var usertoRecid = getQueryString("torecid");
            var usertoGroupid = getQueryString("groupid");
            if (usertoRecid != "") {
                if(usertoGroupid !="")
                    result = chatblog.usercate.chRoom.GetUserid(usertoRecid, usertoGroupid);
                else
                    result = chatblog.usercate.chRoom.GetUserid0(usertoRecid);
                
                if (result != null && result.value != null) {   
                    currentRoomUserTo = result.value[0];
                    document.title = "与 " + result.value[1] + " 交谈中";
                    $(".chatnavDiv > span")[0].innerHTML = document.title;
                }
            }
        }
        catch (e) { }
        
        try {

            //方法来自 editor
            seteditorFontShowTopEle("chatbodydisplayDiv");

            //方法来自 editor
            seteditorKeyDownFunc(
            function(thisObj) {
                sendMsgHotKey = getSendMsgHotKeyCookie();
                if ($('#sendMessageButton')[0].disabled == false) {
                    //Enter:13
                    if (sendMsgHotKey == "Enter" && thisObj.ctrlKey != true) {
                        if (thisObj.keyCode == "13") {
                            $('#sendMessageButton')[0].click();
                            //clearEventBubble(thisObj);
                            frameobj.focus();
                            return false;
                        }
                    }
                    else {                          //CtrlEnter
                        if (thisObj.keyCode == "13" && thisObj.ctrlKey == true) {
                            $('#sendMessageButton')[0].click();
                            //clearEventBubble(thisObj);
                            frameobj.focus();
                            return false;
                        }
                    }

                }
                return true;
            }
        );
            //方法来自 editor
            seteditorKeyUpFunc(
            function(htmlStr) {
                var sendMsgButton = $('#sendMessageButton')[0];
                sendMsgButton.disabled = (htmlStr == "") ? true : false;
                sendMsgButton.title = (htmlStr == "") ? "不能发送空字符串或者空格字符串！" : "点击发送消息！";
            }
        );

        }
        catch (e) { }
    }

    setsendMsgEnableFunc(function(enabledSendMsgButton) {
        $("#sendMessageButton")[0].disabled = !enabledSendMsgButton;
    }
    );
}


init(true); 
if (!(SysAgent.ie && (SysAgent.ie.toString().indexOf('6.') >= 0))) {
    window.onresize = function() {
        init(false);
    }      
}



window.onunload = function() {
    if (parentClose != true && window.opener != null) {
        window.opener.newWindowManager.Remove(name, false, false);
    }
    var user_to_recid = getQueryString("torecid");
    chatblog.usercate.dialog.newRoom.RemoveCurrentChatRoom(user_to_recid);
}

//页面点击事件
document.body.onclick = function(e) {
    e = e || window.event;
    var eventObj = e.target || e.srcElement;
    if (eventObj.id != "hotKeysButton")
        hideHotKeySelect();
}


//点击隐藏和显示  
function showchatBodyRightTd(thisObj) {
    var chatbodyrightTd = $("#chatbodyRightTd")[0];
    var chatbodytable = $(".chatbodyTable")[0];
    var width = 0;
    if (parseInt(chatbodyrightTd.offsetWidth) < 10) {
        chatbodyrightTd.style.background = "white";
        chatbodyrightTd.style.width = "150px";
        chatbodyrightTd.style.border = "solid 1px #4D95EB";
        width = parseInt(chatbodytable.style.width) - 10 - 150 + "px";
        thisObj.src = "../img/right.ico";
        thisObj.title = "点击隐藏";      
    }
    else {
        chatbodyrightTd.style.background = "lightblue";
        chatbodyrightTd.style.width = "0px";
        chatbodyrightTd.style.border = "none";
        width = parseInt(chatbodytable.style.width) - 10 - 10 + "px";
        if (!SysAgent.ie)
            width = parseInt(width) - 5 + "px";
        thisObj.src = "../img/left.ico";
        thisObj.title = "点击显示";
    }
    //方法来自 editor
    setEditorDivClass(width, "none");
    var chatbodydisplaydiv = $("#chatbodydisplayDiv")[0];
    chatbodydisplaydiv.style.width = width;
}
//关闭页面
function closePage() {
    window.opener = null;
    window.open("", "_self");
    window.close();
}

//发送消息
function sendMessage() {
    if (validateUser()==false  ) {
        alert("对不起，您还未登录，请先登录！");
        return;
    }
    var message = getEditorHtml();
    if (message == "") {
        alert("对不起，不能发送空消息！");
        return;
    }
    chatblog.usercate.chRoom.InsertNewMessage(currentRoomUserTo, message, 0);
    frameobj.document.body.innerHTML = "";  //(isIE()) ? "<div></div>" : "";  //在keydown 中设置了回车键 插入 /n
    frameobj.focus();
}

//获取消息并在消息显示窗口显示
function initchatBodyDisplay() {
    if (validateUser() == true) {
        var result = chatblog.usercate.chRoom.GetPersonalChatInfoHtml(currentRoomUserTo);
        var htmlStr = (result.value != "" && result.value != null) ? result.value : "";
        var chatcontentObj = $("#chatbodydisplayDiv")[0];
        if (htmlStr != "") {            
            chatcontentObj.innerHTML = _MsgDivHeader;
            if (currentUserToOnline == false)
                chatcontentObj.innerHTML += _MsgNotOnLineTip;
            chatcontentObj.innerHTML += currentChatInfo + htmlStr + _MsgDivFooter;
            currentChatInfo += htmlStr;
            //$("#chatContentBottom")[0].innerHTML = "最后一条消息收于：" + getCurrentDate();
            chatcontentObj.innerHTML += _MsgEndHTML;
            $('#2ffa7bd7259343babdfae91f9d2250')[0].scrollIntoView(); ;
        }
        else {
            currentUserToOnline = chatblog.usercate.chRoom.getUserStatus(currentRoomUserTo).value; 
            if(chatcontentObj.innerHTML == "" &&currentUserToOnline == false)
                chatcontentObj.innerHTML = _MsgDivHeader + _MsgNotOnLineTip + _MsgDivFooter;   
        }
    } 
}

setInterval(initchatBodyDisplay, 1000);

function validateUser() {
    return !(currentRoomUser == null || currentRoomUser == "" || currentRoomUserTo == null || currentRoomUserTo == "");
}


